<?php

	if($offer['offer_id'] == 0){
		echo '<span class="heading">Add New Item</span>';
	}
	else{
		echo '<span class="heading">Item Management</span>';
	}
	echo '<br />';
	echo '<div class="container">';
	echo '<div id="form-holder">';
	echo form_open_multipart('itemmanagement/'.$offer['offer_id'], array('id' => 'itemmanagement_form'));
	
	echo '<div class="holder">';
	echo '<div id="lhs-form">';
	echo form_label("Name: ", "item_name");
	$item_name_attributes = array(
		'name'		=> 'item_name',
		'id' 			=> 'item_name',
		'size' 		=> '32',
		'value' 		=> set_value('item_name',$offer['item_name']),
		'placeholder' => 'Name of Item',
	);
	echo form_input($item_name_attributes);
	echo form_error('item_name');
	echo '<br />';
	
	
	
	echo form_label("Age: ", "age");
	$year_options = array(
		'0'	=> '0',
		'1' => '1',
		'2'	=> '2',
		'3'	=> '3',
		'4' => '>3'
	);
	$month_options = array(
		'0'	=> '0',
		'1' => '1',
		'2'	=> '2',
		'3'	=> '3',
		'4' => '4',
		'5' => '5',
		'6' => '6',
		'7' => '7',
		'8' => '8',
		'9' => '9',
		'10' => '10',
		'11' => '11'
	);
	echo form_dropdown('age_year', $year_options, set_value('age_year', $offer['age_year']), 'id="age_year" onChange = checkAge();');
	echo form_error('age_year');
	echo form_label(' year(s) &nbsp&nbsp', 'year(s)', array('id' => 'age_year_label'));
	echo form_dropdown('age_month', $month_options, set_value('age_month', $offer['age_month']), 'id="age_month"');
	echo form_error('age_month');
	echo form_label(' month(s) ', 'month(s)', array('id' => 'age_month_label'));
	echo '<br />';
	
	
	
	echo '<div id="item_price_holder">';
	echo form_label("Item Price: ", "item_price");

	if (set_value('item_price', $offer['item_price']) == 0) {
		echo "<input type='radio' id='price_free' name ='price_choice' value='0' " . set_radio('price_choice','0') . " onchange='changePrice()' checked> ".form_label("Free ", "free") ."&nbsp&nbsp"; 
		echo "<input type='radio' id='price_cost' name ='price_choice' value='1' " . set_radio('price_choice','1') . " onchange='changePrice()'> " . form_label("Cost: $ ", "cost");
	}
	else {
		echo "<input type='radio' id='price_free' name ='price_choice' value='0' " . set_radio('price_choice','0') . " onchange='changePrice()'> ".form_label("Free ", "free") . "&nbsp&nbsp"; 
		echo "<input type='radio' id='price_cost' name ='price_choice' value='1' " . set_radio('price_choice','1') . " onchange='changePrice()' checked> " . form_label("Cost: $ ", "cost");
	}
	$item_price_attributes = array(
		'name'		=> 'item_price',
		'id' 			=> 'item_price',
		'size' 		=> '8',
		'value' 		=> set_value('item_price', $offer['item_price']),
		'placeholder' => 'Price of Item',
		'onfocus' => 'changePriceSelector()',
	);
	echo form_input($item_price_attributes);
	echo '<br />'; 
	echo form_error('item_price');
	echo '</div>';
	echo '<br />';

	
	
	
	$category_options[''] = '---';
	foreach($categories AS $cat){
		$category_options[$cat['category_id']] = $cat['category_name'];
	}

	if ($offer['category_id'] == ''){
		$sub_category_options[''] = '---';
	}
	else{
		foreach($sub_categories AS $sub){
			if($offer['category_id'] == $sub['category_id']){
				$sub_category_options[$sub['sub_category_id']] = $sub['sub_category_name'];
			}
		}
	}
	
	echo form_label('Category:&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp', 'category');
	
	echo form_dropdown('category_id', $category_options, set_value('category_id',$offer['category_id']), 'id = "category_id" onChange=changeSubCategory()');
	echo form_error('category_id');
	echo '<br />';
	
	echo form_label('Sub-Category: ', 'sub_category');
	echo form_dropdown('sub_category_id', $sub_category_options, set_value('sub_category_id',$offer['sub_category_id']), 'id = "sub_category_id"');
	echo form_error('sub_category_id');
	echo '<br /><br /><br />';
	
	
	
	echo form_label("Item Condition: ", "item_condition");
	echo '<span id="star-rating"></span>';
	echo '<input type="hidden" id="item_condition" name="item_condition" value="' . set_value('item_condition',$offer['item_condition']) . '" />' ;
	echo form_error('item_condition');
	echo '<br /><br />';
	echo '</div>';
	
	
	
	
	echo '<div id="rhs-form">';
	echo '<div class="image-200"><img class="resize-200" src = "../resources/item_images/' . $offer['item_pic'] .'" /></div>';
	echo '<span class="small-text"> Maximum file size is 3 Mb. </span>';
	echo form_upload(array('name' => 'userfile', 'id' => 'userfile')) . '<br />';
	echo form_submit('upload_pic', 'Upload Picture');
	if (isset($upload_file_error)){
		echo '<br />' . $upload_file_error;
	}
	echo '<input type="hidden" id="item_pic" name="item_pic" value="' . $offer['item_pic'] . '" />' ;
	echo '</div>';
	echo '<div style="clear: both;"></div>';
	echo '</div>';
	
	echo '<div id="desc-holder">';
	echo form_label("Item Description: ", "item_description") . '<br />';
	$item_description_attributes = array(
		'name'		=> 'item_description',
		'id' 			=> 'item_description',
		'rows' 		=> '4',
		'cols'			=> '85',
		'value' 		=> set_value('item_description',$offer['item_description']),
		'placeholder' => 'Description',
	);
	echo form_textarea($item_description_attributes);
	echo '<br />';
	echo '<div id="submit-holder">';
	if($offer['offer_id'] == 0){
		echo form_submit('submit', 'Create new Item');
	}
	else{
		echo form_submit('submit', 'Update Item');
	}
	echo '</div>';
	echo '</div>';
	echo form_close();
	
	echo '</div>';
	echo '</div>';
	//pass php variables to javascript
	$json = json_encode($sub_categories);
	echo '<script type="text/javascript">
				var sub_category_options = ' . $json . ';
				var item_condition = ' . set_value('item_condition',$offer['item_condition']) . ';
			</script>';
?>